package com.jari.lg.service.profile.device;

import com.jari.lg.domain.CrossBase;
import com.jari.lg.domain.DetectorDayEntity;
import com.jari.lg.domain.DevBase;
import com.jari.lg.mapper.DetectorDayEntityMapper;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DetectorDayHisService {

    @Autowired
    private DetectorDayEntityMapper detectorDayEntityMapper;

    public void makeInfo(){
        DetectorDayEntity entity;
        List<DevBase> devBases = BaseData.videoDetectorList;
        List<CrossBase> crossBaseList = BaseData.crossBaseList;
        for(DevBase devBase : devBases){
            entity = generate(devBase, crossBaseList);
            if(null != entity){
                detectorDayEntityMapper.insert(entity);
            }
        }
    }

    private DetectorDayEntity generate(DevBase devBase, List<CrossBase> crossBaseList){
        DetectorDayEntity entity = new DetectorDayEntity();
        entity.setAvgqueue(Utils.makeDouble(100));
        entity.setAvgspeed(Utils.makeDouble(50));
        entity.setBigcar(Utils.makeRandInt(300));
        entity.setDetectorcode(devBase.getDevicecode());
        entity.setMaxqueue(entity.getAvgqueue() +50);
        entity.setLittlecar(Utils.makeRandInt(500));
        entity.setPassingcarnum(entity.getBigcar() + entity.getLittlecar());
        entity.setStartdate(new Date());
        if(StringUtils.isNotBlank(devBase.getCrosscode())){
            entity.setJnccode(devBase.getCrosscode());
            entity.setJncname(crossBaseList.stream().filter(x -> x.getCrosscode().equals(devBase.getCrosscode())).findFirst().get().getCrossname());
            return entity;
        }
        return null;
    }
}
